package client.comm.baoding.ui

import android.Manifest
import android.app.AlertDialog
import android.content.pm.PackageManager
import android.graphics.Color
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.style.ForegroundColorSpan
import android.util.Log
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import client.comm.baoding.R
import client.comm.baoding.databinding.ActivityTestBinding
import client.comm.baoding.ui.vm.TestViewModel
import client.comm.commlib.base.BaseActivity
import com.amap.api.location.AMapLocation
import com.amap.api.location.AMapLocationClient
import com.amap.api.location.AMapLocationClientOption
import com.amap.api.location.AMapLocationListener
import com.amap.api.maps.MapsInitializer
import com.google.android.exoplayer2.MediaItem
import com.google.android.exoplayer2.SimpleExoPlayer


class TestActivity : BaseActivity<TestViewModel, ActivityTestBinding>() {
    override fun getLayout(): Int {
        return R.layout.activity_test
    }

    override fun initView() {
        binding.event = this
//        initPlayer()
    }

    val player by lazy {
        SimpleExoPlayer.Builder(this)
            .build()
    }

    fun ttt() {
//        requestExternalStorage()
        privacyCompliance()
    }


    private fun privacyCompliance() {
        MapsInitializer.updatePrivacyShow(this@TestActivity, true, true)
        val spannable =
            SpannableStringBuilder("\"请同意获取定位权限；\n")
        spannable.setSpan(
            ForegroundColorSpan(Color.BLUE),
            35,
            42,
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
        )
        AlertDialog.Builder(this)
            .setTitle("提示")
            .setMessage(spannable)
            .setPositiveButton(
                "同意"
            ) { dialogInterface, i -> MapsInitializer.updatePrivacyAgree(this@TestActivity, true) }
            .setNegativeButton(
                "不同意"
            ) { dialogInterface, i ->
                MapsInitializer.updatePrivacyAgree(
                    this@TestActivity,
                    false
                )
            }
            .show()
    }

    fun runHasPermis() {

    }

    fun requestExternalStorage() {
        if (ContextCompat.checkSelfPermission(
                this,
                Manifest.permission.WRITE_EXTERNAL_STORAGE
            ) == PackageManager.PERMISSION_GRANTED
        ) {//有權限
            runHasPermis()
        } else {
            ActivityCompat.requestPermissions(
                this,
                arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE),
                0x99
            )
        }
    }

    fun initPlayer() {

        //填充播放列表
        val mediaItem =
            MediaItem.fromUri("http://wxsnsdy.tc.qq.com/105/20210/snsdyvideodownload?filekey=30280201010421301f0201690402534804102ca905ce620b1241b726bc41dcff44e00204012882540400&bizid=1023&hy=SH&fileparam=302c020101042530230204136ffd93020457e3c4ff02024ef202031e8d7f02030f42400204045a320a0201000400")
        player.addMediaItem(mediaItem)
        player.prepare()
        player.play()
    }

    override fun initViewModel(): TestViewModel = createViewModel()

    override fun onResume() {
        super.onResume()
        player.play()
    }

    override fun onPause() {
        super.onPause()
        player.pause()
    }

    override fun onDestroy() {
        super.onDestroy()
        player.release()
        destroyLocation()
    }

    var mLocationClient: AMapLocationClient? = null
    private fun destroyLocation() {
        if (null != mLocationClient) {
            /**
             * 如果AMapLocationClient是在当前Activity实例化的，
             * 在Activity的onDestroy中一定要执行AMapLocationClient的onDestroy
             */
            mLocationClient?.onDestroy()
            mLocationClient = null
            mLocationClient = null
        }
    }

    fun change() {

        //声明AMapLocationClient类对象
        //声明AMapLocationClient类对象
        //声明定位回调监听器
        //初始化定位
        mLocationClient = AMapLocationClient(applicationContext)
        //设置定位回调监听
        //设置定位回调监听
        mLocationClient?.setLocationListener(object : AMapLocationListener {
            override fun onLocationChanged(p0: AMapLocation?) {
                Log.d("ddd", "${p0?.city}")
            }
        })

        //声明AMapLocationClientOption对象
        //声明AMapLocationClientOption对象
        var mLocationOption: AMapLocationClientOption? = null
        //初始化AMapLocationClientOption对象
        //初始化AMapLocationClientOption对象
        mLocationOption = AMapLocationClientOption()

        mLocationOption.setLocationPurpose(AMapLocationClientOption.AMapLocationPurpose.SignIn);

        mLocationClient?.setLocationOption(mLocationOption);


        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);

        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Battery_Saving);

        mLocationOption.setOnceLocation(true);

        //获取最近3s内精度最高的一次定位结果：
        mLocationOption.setOnceLocationLatest(true);

        mLocationOption.setInterval(1000);
        //设置是否返回地址信息（默认返回地址信息）
        mLocationOption.setNeedAddress(true);

        //设置是否允许模拟位置,默认为true，允许模拟位置
        mLocationOption.setMockEnable(true);

        //单位是毫秒，默认30000毫秒，建议超时时间不要低于8000毫秒。
        mLocationOption.setHttpTimeOut(20000);

        //关闭缓存机制
        mLocationOption.setLocationCacheEnable(false);


        //给定位客户端对象设置定位参数
        mLocationClient?.setLocationOption(mLocationOption);
        //启动定位
        mLocationClient?.startLocation();
    }
}